//自动生成路由
//解析成模块 eager:将皮膜人头函数 变成模块

import type { RouteRecordRaw } from 'vue-router';

const routers = import.meta.glob('../pages/**/meta.ts', { eager: true });
const cpnParse = import.meta.glob('../pages/**/index.vue');
let arr = Object.entries(routers).map(([path, meta]) => {
  let newPath = path.replace('../pages/', '/').replace('/meta.ts', '');
  console.log(path);

  //页面组件的位置
  let cpnPath = path.replace('meta.ts', 'index.vue');

  //路由name
  let name = newPath.split('/').filter(Boolean).join('-');

  //  组件 懒加载引入
  let cpnImportFn = cpnParse[cpnPath];

  let res: RouteRecordRaw = {
    path: newPath || '/index',
    name,
    //@ts-ignore
    meta: meta.default || {},
    component: cpnImportFn,
    //打包 模块分析 写变量就分析不了
  };
  return res;
});
//访问意料之外的路由时重定向到)
//@ts-ignore
arr.push({ path: '/:catchAll(.*)', redirect: '/index' });

export default arr;
